package com.lottery.service.pc28.lobbyService.impl;

import com.alibaba.fastjson.JSON;
import com.lottery.dao.pc28.lobbyDao.AdpictureDao;
import com.lottery.dao.pc28.lobbyDao.LotteryDao;
import com.lottery.dao.pc28.lobbyDao.LotteryOddsDao;
import com.lottery.entity.Adpicture;
import com.lottery.entity.Lottery;
import com.lottery.entity.LotteryOdds;
import com.lottery.service.pc28.lobbyService.LotteryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//分页查询往期开奖记录
@Service
@Transactional
public class LotteryServiceImpl implements LotteryService {

	@Autowired
	private LotteryDao lotteryDao;

	@Autowired
	private AdpictureDao adpictureDao;

	@Autowired
	private LotteryOddsDao lotteryOddsDao;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 分页查询往期开奖记录
	 * 
	 * @param pageNow
	 *            当前页数
	 * @param pageSize
	 *            每页条数
	 * @return
	 */
	@Override
	public String selectLotteryList(int pageNow, int pageSize, String expect, int lotteyType) {
		Map<String, Object> maxmap = new HashMap<>();
		List<Map<String, String>> list = new ArrayList<>();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			int startPage = ((pageNow - 1) * pageSize);
			List<Lottery> selectLotteryList = lotteryDao.selectLotteryList(startPage, pageSize, expect, lotteyType);
			for (int i = 0; i < selectLotteryList.size(); i++) {
				Map<String, String> map = new HashMap<String, String>();
				Lottery lottery = selectLotteryList.get(i);
				map.put("expect", lottery.getExpect());
				String type = String.valueOf(lotteyType);
				map.put("lotteyType", type);
				String replaceAll = lottery.getOpencode().replaceAll(",", "");
				map.put("opencode", replaceAll);
				String replace = lottery.getOpencode();
				String[] str = replace.split(",");
				String rule = "";
				if (lotteyType == 1) {
					int amount = Integer.parseInt(str[0]) + Integer.parseInt(str[1]) + Integer.parseInt(str[2])
							+ Integer.parseInt(str[3]) + Integer.parseInt(str[4]);
					if (amount >= 23 && amount % 2 != 0) {
						rule = "大单";
					} else if (amount < 23 && amount % 2 != 0) {
						rule = "小单";
					} else if (amount >= 23 && amount % 2 == 0) {
						rule = "大双";
					} else if (amount < 23 && amount % 2 == 0) {
						rule = "小双";
					}
				} else if (lotteyType == 2) {
					int amount = Integer.parseInt(str[0]) + Integer.parseInt(str[1]) + Integer.parseInt(str[2]);
					if (amount > 13 && amount % 2 != 0) {
						rule = "大单";
					} else if (amount <= 13 && amount % 2 != 0) {
						rule = "小单";
					} else if (amount >= 14 && amount % 2 == 0) {
						rule = "大双";
					} else if (amount < 14 && amount % 2 == 0) {
						rule = "小双";
					}
				}

				map.put("playInfo", rule);
				String format = simpleDateFormat.format(lottery.getOpentime());
				String[] split = format.split(" ");
				map.put("yeartime", split[0]);
				map.put("hourtime", split[1]);
				list.add(map);
			}
			maxmap.put("array", list);
			maxmap.put("result", "0");
			maxmap.put("msg", "成功");
		} catch (Exception e) {
			logger.error("selectLotteryList error", e);
			maxmap.put("result", "1");
			maxmap.put("msg", "失败");
		}
		return JSON.toJSONString(maxmap, true);
	}

	@Override
	public String getAdpicturer() {
		Map<String, Object> maxmap = new HashMap<>();
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			List<Adpicture> ads = adpictureDao.getPictureUrls(1);
			for (Adpicture one : ads) {
				Map<String, Object> map = new HashMap<>();
				map.put("pictureUrl", one.getPictureUrl());
				map.put("content",one.getRemark());
				list.add(map);
			}
			maxmap.put("array", list);
			maxmap.put("result", "0");
			maxmap.put("msg", "成功");
		} catch (Exception e) {
			logger.error("selectLotteryList error", e);
			maxmap.put("result", "1");
			maxmap.put("msg", "失败");
		}
		return JSON.toJSONString(maxmap, true);
	}

	@Override
	public String getLotteryOdds() {
		// TODO Auto-generated method stub
		Map<String, Object> maxmap = new HashMap<>();
		List<Map<String, String>> list = new ArrayList<>();
		try {
			List<LotteryOdds> lotteryOdds = lotteryOddsDao.selectLotteryOddsListBytype(1);
			for (LotteryOdds one : lotteryOdds) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("playName", one.getBetWay());
				double odd = one.getOdds() - one.getOdds() * one.getRatio() + 1;
				map.put("payMultiple", Double.toString(odd));
				list.add(map);
			}
			maxmap.put("ssc_PayRule", list);
			maxmap.put("result", "0");
			maxmap.put("msg", "成功");
		} catch (Exception e) {
			logger.error("selectLotteryList error", e);
			maxmap.put("result", "1");
			maxmap.put("msg", "失败");
		}
		return JSON.toJSONString(maxmap, true);
	}
}
